package main

import (
	"flag"
	"fmt"
	"iot-base/common/cache"
	"iot-base/common/notify"
	"iot-base/common/propertytype"
	"math/rand"
	"time"
)

func init() {
	flag.StringVar(&id, "tid", "01bf31a6-0b54-4fd9-bb42-5eb162cf8719", "dev id")
	flag.StringVar(&pid, "pid", "stream", "property id")
	flag.IntVar(&index, "index", 0, "index")
	flag.StringVar(&value, "value", "", "value")
	flag.StringVar(&sid, "sid", "", "subid")
	//flag.StringVar(&type,"type","cloud","类型")
}

var id string
var pid string
var sid string
var index int
var value string
var Rander = rand.New(rand.NewSource(time.Now().UnixNano()))

func randomData(pProp *cache.ItemInfo) interface{} {
	max := 100.0
	min := 0.0
	var target int64
	if pProp.HasRange {
		max = pProp.Max
		min = pProp.Min

	}
	target = int64((int(min) + Rander.Intn(int(max-min+1))))
	switch pProp.Type {
	case "int":
		return int64(target)
	case "float", "float32", "float64":
		return target
	case "enum":
		if pProp.EnumList == nil {
			return 0
		}
		array := *pProp.EnumList
		len := len(array)
		if len > 0 {
			index := Rander.Intn(len)
			return array[index]
		}
		return 0
	case "text", "stream":
		return fmt.Sprintf("%v", target)
	}

	return nil

}

func structArrayRandom(values [][]propertytype.StructValue, id string, pid string, props []*cache.ItemInfo) {

	for i := 0; i < 2; i++ {
		values[i] = make([]propertytype.StructValue, len(props))
		for j, pProp := range props {
			values[i][j].SubID = pProp.SubID
			values[i][j].Value = randomData(pProp)
		}
	}

}

func structRandom(values []propertytype.StructValue, props []*cache.ItemInfo) {
	//values[i] = make([]propertytype.StructValue, len(props))
	for i, pProp := range props {
		values[i].SubID = pProp.SubID
		values[i].Value = randomData(pProp)
	}

}
func makeValue(pThing *cache.ThingInfo, pProp *cache.ItemInfo) {
	//thing, err := cache.GetThing(id)
	//if err == nil {
	//isExist, pProp := cache.GetModelProperty(thing.Thing.ModelID, pid, "")

	if pProp != nil {
		var pid = pProp.PID
		if pProp.Type == "array" {
			ok, props := cache.GetChildProperties(pThing.Thing.ModelID, pid)
			if ok {
				if len(props) == 1 {
					pArray := make([]interface{}, 2)
					pData := &propertytype.Value{
						TID:        id,
						PID:        pid,
						Value:      pArray,
						PickTime:   time.Now().Unix(),
						ReportTime: time.Now().Unix(),
					}
					for i := 0; i < 2; i++ {
						pArray[i] = randomData(props[0])
					}
					fmt.Println("---------******--------", pData)
					notify.RawDataEx(pData)

				} else {
					pArray := make([][]propertytype.StructValue, 2)
					pData := &propertytype.Value{
						TID:        id,
						PID:        pid,
						Value:      pArray,
						PickTime:   time.Now().Unix(),
						ReportTime: time.Now().Unix(),
					}
					structArrayRandom(pArray, id, pid, props)
					fmt.Println("---------******--------", pData)
					notify.RawDataEx(pData)

				}

			}

		}
		if pProp.Type == "struct" {
			ok, props := cache.GetChildProperties(pThing.Thing.ModelID, pid)
			if ok {
				pStruct := make([]propertytype.StructValue, len(props))
				pData := &propertytype.Value{
					TID:        id,
					PID:        pid,
					Value:      pStruct,
					PickTime:   time.Now().Unix(),
					ReportTime: time.Now().Unix(),
				}
				structRandom(pStruct, props)
				notify.RawDataEx(pData)
				fmt.Println("---------******--------", pData)
			} else {
				fmt.Println("GetChildProperties -------error")
			}
		} else {
			pData := &propertytype.Value{
				TID:        id,
				PID:        pid,
				Value:      randomData(pProp),
				PickTime:   time.Now().Unix(),
				ReportTime: time.Now().Unix(),
			}
			notify.RawDataEx(pData)
		}

	} else {
		fmt.Println("get property -------error")
	}

}
func main() {
	//rand.Seed(time.Now().UnixNano())
	flag.Parse() //暂停获取参数
	pThing, _ := cache.GetThing(id)
	if pid == "" {
		if pThing != nil {
			ok, props := cache.GetChildProperties(pThing.Thing.ModelID, "")
			if ok {
				for _, p := range props {
					fmt.Println("make data ----------", p)
					makeValue(pThing, p)
				}
			} else {
				fmt.Println("found no props----------")
			}
		} else {
			fmt.Println("tid error--------------")
		}
	} else if value == "" {
		_, prop := cache.GetModelProperty(pThing.Thing.ModelID, pid, "")
		makeValue(pThing, prop)
	} else {
		notify.RawData(id, pid, sid, index, value, 0, 0)
	}
}
